﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Diagnostics;

namespace GameDevelopHelper.Common
{
    public class Configer
    {
        #region
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
        
        #endregion

        private static string m_strConfigPath = Application.StartupPath + "\\Config.ini";
        public Configer()
        {

        }

        // 写字符串
        protected static void WriteString(string strSection, string strKey, string strValue)
        {
            WritePrivateProfileString(strSection, strKey, strValue, m_strConfigPath);
        }

        // 读字符串
        protected static string ReadString(string strSection, string strKey, string strDefaultValue)
        { 
            StringBuilder str = new StringBuilder();
            GetPrivateProfileString(strSection, strKey, strDefaultValue, str, str.MaxCapacity, m_strConfigPath);
            return str.ToString();
        }

        //写int
        protected static void WriteInteger(string strSection, string strKey, int nValue)
        {
            WritePrivateProfileString(strSection, strKey, Convert.ToString(nValue), m_strConfigPath);
        }

        //读int
        protected static int ReadInteger(string strSection, string strKey, int bDefaultValue)
        {
            StringBuilder str = new StringBuilder();
            GetPrivateProfileString(strSection,
                strKey,
                Convert.ToString(bDefaultValue),
                str,
                str.MaxCapacity,
                m_strConfigPath
                );

            return Convert.ToInt32(str.ToString());
        }

        //写bool
        protected static void WriteBoolean(string strSection, string strKey, bool bValue)
        {
            WritePrivateProfileString(strSection, strKey, bValue == true ? "1" : "0", m_strConfigPath);
        }

        //读bool
        protected static bool ReadBoolean(string strSection, string strKey, bool bDefaultValue)
        {
            StringBuilder str = new StringBuilder();
            GetPrivateProfileString(strSection,
                strKey,
                Convert.ToString(bDefaultValue), 
                str, 
                str.MaxCapacity,
                m_strConfigPath
                );


            return str.ToString() == "1" ? true : false;
        }

        /// <summary>
        /// 简单异或加解密算法
        /// </summary>
        /// <param name="strPlainText">要加密或者解密的字符串</param>
        /// <param name="strKey">密钥</param>
        /// <returns>返回密文/明文</returns>
        protected static string XorEncrypt(string strText, byte strKey)
        {
            byte[] bt = Encoding.Default.GetBytes(strText);
            for (int i = 0; i < bt.Length; i+=2)
            {
                bt[i] = (byte)(bt[i] ^ strKey);
            }

            Debug.Print("Encrypt/Decrypt:" + Encoding.Default.GetString(bt));
            return Encoding.Default.GetString(bt);
        }
    }
    
    /// <summary>
    /// 登录配置
    /// </summary>
    public class LoginConfig : Configer
    {
        private const string m_strLoginSection = "Login";
        private const byte m_strEncryptKey = 125;

        public static bool bAutoLogin = false;
        public static bool bRememberPassword = false;
        public static string strUsername = "";
        public static string strPassword = "";
        public static int nProjectId = -1;

        /// <summary>
        /// 保存自动登录设置
        /// </summary>
        /// <param name="bValue"></param>
        public static void SaveAutoLogin(bool bValue)
        {
            WriteBoolean(m_strLoginSection, "AutoLogin", bValue);
        }

        /// <summary>
        /// 读取自动登录设置
        /// </summary>
        public static bool GetAutoLogin()
        {
            return ReadBoolean(m_strLoginSection, "AutoLogin", false);
        }

        /// <summary>
        /// 保存记住密码设置
        /// </summary>
        /// <param name="bValue"></param>
        public static void SaveRememberPassword(bool bValue)
        {
            WriteBoolean(m_strLoginSection, "RememberPassword", bValue);
        }

        /// <summary>
        /// 读取保存密码设置
        /// </summary>
        /// <returns></returns>
        public static bool GetRememberPassword()
        {
            return ReadBoolean(m_strLoginSection, "RememberPassword", false);
        }

        /// <summary>
        /// 保存登录用户名
        /// </summary>
        /// <param name="strUsername"></param>
        public static void SaveLoginUsername(string strUsername)
        {
            WriteString(m_strLoginSection, "Username", strUsername);
        }

        /// <summary>
        /// 读取登录用户名
        /// </summary>
        /// <returns></returns>
        public static string GetLoginUsername()
        {
            return ReadString(m_strLoginSection, "Username", "");
        }

        /// <summary>
        /// 保存登录密码
        /// </summary>
        /// <param name="strPassword"></param>
        public static void SaveLoginPassword(string strPassword)
        {
            WriteString(m_strLoginSection, "Password", XorEncrypt(strPassword, m_strEncryptKey));
        }

        /// <summary>
        /// 读取登录密码
        /// </summary>
        /// <returns></returns>
        public static string GetLoginPassword()
        {
            string strPassword = ReadString(m_strLoginSection, "Password", "");
            return XorEncrypt(strPassword, m_strEncryptKey);
        }

        /// <summary>
        /// 保存默认项目ID
        /// </summary>
        /// <param name="id"></param>
        public static void SaveProjectId(int id)
        {
            WriteInteger(m_strLoginSection, "ProjectId", id);
        }

        /// <summary>
        /// 获取项目ID
        /// </summary>
        /// <returns></returns>
        public static int GetProjectId()
        {
            return ReadInteger(m_strLoginSection, "ProjectId", -1);
        }
    }
}
